local panxuan = fk.CreateSkill{
  name = "dajubei__panxuan",
}

Fk:loadTranslationTable{
  ["dajubei__panxuan"] = "叛旋",
  [":dajubei__panxuan"] = "每回合限一次，当你需要使用基本牌时，你可以交给一名其他角色一张牌，然后你观看其手牌并可使用其中的对应牌。"
  .."若你未使用，你获得其场上一张牌。",

  ["panxuanPoxi"] = "叛旋",

  ["#dajubei__panxuan"] = "叛旋：可以视为使用一张基本牌",
  ["#dajubei__panxuan-give"] = "叛旋：将一张牌交给一名其他角色，然后你观看其手牌并可使用其中的对应牌",
  ["#dajubei__panxuan-use"] = "叛旋：使用一张 %src 手牌中的一张对应牌，或获得其场上的一张牌",
}

-- Poxi
Fk:addPoxiMethod{
  name = "panxuanPoxi",
  prompt = function (data, extra_data)
    return "#dajubei__panxuan-use:" .. extra_data.target
  end,
  card_filter = function (to_select, selected, data, extra_data)
    if #selected == 0 then
      return table.contains(extra_data.ableCards, to_select)
    end
  end,
  feasible = function (selected, data, extra_data)
    return #selected == 1 and table.contains(extra_data.ableCards, selected[1])
  end,
  default_choice = function (data, extra_data)
    return extra_data.ableCards[1]
  end,
}

-- view cards and choose one
---@param player ServerPlayer
---@param target ServerPlayer
---@param name string
---@return integer[]
local function viewAndChoose(player, target, name)
  local room = player.room
  local card_data = {}
  local ableCards = {}

  for i, flag in ipairs{"h", "e", "j"} do
    local areas = {"$Hand", "$Equip", "$Judge"}
    local cards = target:getCardIds(flag)
    if #cards > 0 then
      table.insert(card_data, {areas[i], cards})
      if i > 1 then
        table.insertTable(ableCards, cards)
      else
        for _, id in ipairs(cards) do
          if Fk:getCardById(id).trueName == name then
            table.insert(ableCards, id)
          end
        end
      end
    end
  end

  local ret = room:askToPoxi(player, {
    poxi_type = "panxuanPoxi",
    cancelable = false,
    data = card_data,
    extra_data = {
      skillName = "dajubei__panxuan",
      ableCards = ableCards,
      target = target.id,
    }
  })

  return ret
end

---@return string[]
local getAllBasicCardNames = function ()
  local names = {}
  table.forEach(Fk:getAllCardIds(), function (id)
    if Fk:getCardById(id).type == Card.TypeBasic then
      table.insertIfNeed(names, Fk:getCardById(id).trueName)
    end
  end)
  return names
end

panxuan:addEffect("viewas", {
  anim_type = "support",
  prompt = "#dajubei__panxuan",
  pattern = ".|.|.|.|.|basic",
  interaction = function (self, player)
    local all_choices = getAllBasicCardNames()
    local choices = player:getViewAsCardNames(panxuan.name, all_choices)
    return UI.CardNameBox { choices = choices, all_choices = all_choices, }
  end,
  card_filter = Util.FalseFunc,
  view_as = function (self, player, cards)
    if self.interaction.data then
      local card = Fk:cloneCard(self.interaction.data)
      card.skillName = panxuan.name
      return card
    end
  end,
  before_use = function (self, player, use)
    local room = player.room
    local targets, cards = room:askToChooseCardsAndPlayers(player, {
      min_card_num = 1,
      max_card_num = 1,
      targets = room:getOtherPlayers(player, false),
      min_num = 1,
      max_num = 1,
      skill_name = panxuan.name,
      prompt = "#dajubei__panxuan-give",
      cancelable = false,
    })

    local target = targets[1]
    room:moveCardTo(cards, Card.PlayerHand, target, fk.ReasonGive, panxuan.name, nil, true, player)
    local op_cards = viewAndChoose(player, target, use.card.trueName)
    if table.contains(target:getCardIds("h"), op_cards[1]) then
      use.card = Fk:getCardById(op_cards[1])
    else
      for _, flag in ipairs{"e", "j"} do
        if table.contains(target:getCardIds(flag), op_cards[1]) then
          room:moveCardTo(op_cards, Card.PlayerHand, player, fk.ReasonPrey, panxuan.name, nil, true, player)
          return panxuan.name
        end
      end
    end
  end,
  enabled_at_play = function (self, player)
    return player:usedSkillTimes(panxuan.name, Player.HistoryTurn) < 1 and not player:isNude()
  end,
  enabled_at_response = function (self, player, response)
    return player:usedSkillTimes(panxuan.name, Player.HistoryTurn) < 1 and not player:isNude()
    and not response
  end,
})

return panxuan